package com.example.guavaSrudy.BasicEvent.simple;

import com.google.common.eventbus.EventBus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;

@Component
public class SimpleTradeExecutor {

    private EventBus eventBus;

    @Autowired
    public SimpleTradeExecutor(EventBus eventBus) {
        this.eventBus = eventBus;
    }


    public void executeTrade(TradeAccount tradeAccount,double amount, TradeType tradeType) {
        TradeAccountEvent tradeAccountEvent = processTrade(tradeAccount,amount,tradeType);
        eventBus.post(tradeAccountEvent);
    }

    private TradeAccountEvent processTrade(TradeAccount tradeAccount,double amount,TradeType tradeType) {
        Date executionTime = new Date();
        String message = String.format("Processed trade for %s of amount $%s type %s @%s",
                tradeAccount,amount,tradeType,executionTime);
        TradeAccountEvent tradeAccountEvent = new TradeAccountEvent(amount, executionTime, tradeType,tradeAccount);
        System.out.println(message);
        return tradeAccountEvent;
    }
}
